<?php
namespace app\controller\clean;

use app\BaseController;
use think\facade\Db;
use app\logic\RedisLogic;
use app\logic\ProjectLogic;
use app\model\HandleCompany;
use app\model\HandleProjectCases;
use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
use think\Exception;
use think\exception\Handle;

class Stream extends BaseController
{
    /**
     * 新增知识库信息
     * @return \think\response\Json
     */
    public function addZjRenameAiStream()
    {
        $type = 'add_file_change_name_base';
        $post_data = $this->request->post();
        // $post_data = ['file'=>[  'Y018@101@借款合同' ],'type'=>[ '借款合同' ]];
        $insert_data = [];
        $send_data = [];
        foreach ($post_data['file'] as $key => $value) {
            $id = getGuid();
            $send_data['ids'][] = $id;
            $send_data['documents'][] = $value;
            $send_data['metadatas'][] = ['label'=>$post_data['type'][$key]];
            $insert_data[] = ['id' => $id, 'documents' => $value, 'metadatas' => '{"label":"'.$post_data['type'][$key].'"}'];
        }
        
        // 将数据存入数据库 
        $res = Db::name('handle_knowledge')->insertAll($insert_data);
        // dump($res);
        if($res){
            // // 存入数据库成功后，将数据发送到Redis Stream
            $result = RedisLogic::ins()->addTaskStream($type, $send_data,true);
            return json($result);
        }else{
            return json(errorMsg('新增知识库信息失败'));
        }
        
    }

    /**
     * 通过 json 文件批量导入知识库数据到MySQL【临时方案】
     * @return \think\response\Json
     */
    public function addKnowledgeBase()
    {
        $json_data = '';
        $data = json_decode($json_data,true);
        $insert_data = [];
        foreach ($data as $key => $value) {
            $insert_data[$key]['id'] = getGuid();
            $insert_data[$key]['documents'] = trim($value['输入']);
            $insert_data[$key]['metadatas'] = '{"label":"'.trim($value['输出']).'"}';
        }
        $result = Db::name('handle_knowledge')->insertAll($insert_data);
        return json($result);
    }

    /**
     * 将MySQL表中的所有知识库数据导入Redis Stream【临时方案】
     * @return \think\response\Json
     */
    public function allAddZjRenameAiStream()
    {
        $allData = Db::name('handle_knowledge')->select()->toArray();
        $type = 'add_file_change_name_base';
        $insert_data = [
            'ids' => array_column($allData, 'id'),
            'documents' => array_column($allData, 'documents'),
            'metadatas' => array_map(function($item){
                return json_decode($item, true);
            }, array_column($allData, 'metadatas'))
        ];
        // dump($insert_data);
        // 将消息发送到Redis Stream
        $result = RedisLogic::ins()->addTaskStream($type, $insert_data,true);
        return json($result);
    }

    /**
     * 根据MySQL知识库表，清空Redis Stream中的知识库数据【临时方案】
     * @return \think\response\Json
     */
    public function alldelZjRenameAiStream()
    {
        $allData = Db::name('handle_knowledge')->select()->toArray();
        $type = 'delete_file_change_name_base';
        $insert_data = [
            'ids' => array_column($allData, 'id')
        ];
        // dump($insert_data);
        // 将消息发送到Redis Stream
        $result = RedisLogic::ins()->addTaskStream($type, $insert_data,true);
        return json($result);
    }


    // 添项目清洗任务 stream
    public function addChangeFileStream()
    {
        $type = 'file_change_name';
        /* $uuid = $this->request->param('_uuid_');
        $info = HandleProjectCases::ins()->where('project_uuid',$uuid)->value('data_content');
        if (empty($info)) {
            return errorMsg('请先上传项目要素表');
        }
        $clean_data = json_decode($info,true);
        if(count($clean_data) < 1){
            return errorMsg('要素表信息上传有误，请重新上传');
        }
        $root_dir_path = '/data/nas/数据清洗专用/测试专用/文件改名测试';
        $clean_data = ['982103211620042089'=>['982103211620042089'],'982103212359133384'=>['982103212359133384'],'982103220001584354'=>['982103220001584354'],'982103201031198763'=>['982103201031198763']];
        foreach( $clean_data as $jb => $value)
        {
            $data['dir_path'] = $root_dir_path.$jb.'-'.$value[1];
            $result[] = RedisLogic::ins()->addTaskStream($type, $data,true);
        } */


        // test one data
        $root_dir_path = '/data/nas/数据清洗专用/测试专用/文件改名测试2';
        $data['dir_path'] = $root_dir_path.'/982011031206182303-徐春伟';

        $result = RedisLogic::ins()->addTaskStream($type, $data,true);
        return json($result);
    }

    public function testAddClean()
    {
        $json_str = '{"862305221644263981-张连彩":["862305221644263981-张连彩"],"862306081507211488-张宏敏":["862306081507211488-张宏敏"],"982012180938502617-宋丽":["982012180938502617-宋丽"],"982105121625500207-王玲":["982105121625500207-王玲"],"982105160229265603-丁仕雷":["982105160229265603-丁仕雷"],"982105191111030217-丁仕雷":["982105191111030217-丁仕雷"],"982105191114180397-隋世磊":["982105191114180397-隋世磊"],"982105221320151883-王魁":["982105221320151883-王魁"],"982105251216594853-吴坤":["982105251216594853-吴坤"],"982105280931587862-王庆":["982105280931587862-王庆"],"982105311632240421-纪艺勇":["982105311632240421-纪艺勇"],"982105311918046273-李千里":["982105311918046273-李千里"],"982106031859542828-宁晓兰":["982106031859542828-宁晓兰"],"982106081157144184-宁晓兰":["982106081157144184-宁晓兰"],"982106122118078369-宋丽":["982106122118078369-宋丽"],"982106190506438588-李晨":["982106190506438588-李晨"],"982106220655390869-宁晓兰":["982106220655390869-宁晓兰"],"982106231356113837-赵瑞江":["982106231356113837-赵瑞江"],"982106241407092342-王庆":["982106241407092342-王庆"],"982106270918347973-隋世磊":["982106270918347973-隋世磊"],"982106290056492064-刘路":["982106290056492064-刘路"],"982107251026472402-潘记明":["982107251026472402-潘记明"],"982107252100242881-李晨":["982107252100242881-李晨"],"982108021449583570-王峰":["982108021449583570-王峰"],"982108041159043948-王峰":["982108041159043948-王峰"],"982108051157327698-王峰":["982108051157327698-王峰"],"982108080915349827-王玉龙":["982108080915349827-王玉龙"],"982108081400115944-李丹明":["982108081400115944-李丹明"],"982108081510071151-刘根":["982108081510071151-刘根"],"982108131331599390-宋丽":["982108131331599390-宋丽"],"982108151429058067-陈安妮":["982108151429058067-陈安妮"],"982108271428063761-钱康宁":["982108271428063761-钱康宁"],"982109051605383041-杨世刚":["982109051605383041-杨世刚"],"982109051640105872-吕富贵":["982109051640105872-吕富贵"],"982109071334307621-杨世刚":["982109071334307621-杨世刚"],"982110071642261739-詹恒":["982110071642261739-詹恒"],"982112040838501088-唐果":["982112040838501088-唐果"],"982112062134494711-黄晓阳":["982112062134494711-黄晓阳"],"982112091908149998-蔺玉婷":["982112091908149998-蔺玉婷"],"982112130928482490-李一博":["982112130928482490-李一博"],"982112181605264186-胡俊宝":["982112181605264186-胡俊宝"],"982112261104076770-孟祥迎":["982112261104076770-孟祥迎"],"982112271300300977-冯无及":["982112271300300977-冯无及"],"982201010927107312-刘克铭":["982201010927107312-刘克铭"],"982201031530041481-向辉映":["982201031530041481-向辉映"],"982201071640238452-徐建伟":["982201071640238452-徐建伟"],"982201091936508606-丁鹏飞":["982201091936508606-丁鹏飞"],"982201102337092197-路晶":["982201102337092197-路晶"],"982201111155295781-张彦龙":["982201111155295781-张彦龙"],"982201131627515010-李康":["982201131627515010-李康"],"982201131857435208-吴海华":["982201131857435208-吴海华"],"982201132323236884-吴育礼":["982201132323236884-吴育礼"],"982201151758252317-时春阳":["982201151758252317-时春阳"],"982201160222565922-黄越高":["982201160222565922-黄越高"],"982201160909351421-罗阳":["982201160909351421-罗阳"],"982201191242189035-梁云庆":["982201191242189035-梁云庆"],"982201241631133052-路静娟":["982201241631133052-路静娟"],"982201242136007233-史开斌":["982201242136007233-史开斌"],"982201250836117285-臧海鹏":["982201250836117285-臧海鹏"],"982201251352035254-史开斌":["982201251352035254-史开斌"],"982201251726006324-倪铭":["982201251726006324-倪铭"],"982201261127359925-郭金龙":["982201261127359925-郭金龙"],"982201261129574898-冯建新":["982201261129574898-冯建新"],"982201261456566219-刘克铭":["982201261456566219-刘克铭"],"982201272348493968-李思凯":["982201272348493968-李思凯"],"982201281450483794-刘克铭":["982201281450483794-刘克铭"],"982201290918112670-樊文亮":["982201290918112670-樊文亮"],"982201291320246417-樊文亮":["982201291320246417-樊文亮"],"982201292154007344-陈泰安":["982201292154007344-陈泰安"],"982201301159553198-陈泰安":["982201301159553198-陈泰安"],"982201310959223329-桂建平":["982201310959223329-桂建平"],"982201311507225142-厉复伟":["982201311507225142-厉复伟"],"982202011020008222-龙正康":["982202011020008222-龙正康"],"982202021343435092-节仲豪":["982202021343435092-节仲豪"],"982202031056361004-曹文洲":["982202031056361004-曹文洲"],"982202031422328811-唐果":["982202031422328811-唐果"],"982202050952001308-汤孟龙":["982202050952001308-汤孟龙"],"982202051350404143-王全扶":["982202051350404143-王全扶"],"982202061004251381-汤凌辉":["982202061004251381-汤凌辉"],"982202061054211558-王泽宇":["982202061054211558-王泽宇"],"982202070521059479-陈义杨":["982202070521059479-陈义杨"],"982202071242059358-王全扶":["982202071242059358-王全扶"],"982202091319397286-谢娅妮":["982202091319397286-谢娅妮"],"982202100907427039-张其前":["982202100907427039-张其前"],"982202101538530935-陈瑜":["982202101538530935-陈瑜"],"982202120304367023-玄佳":["982202120304367023-玄佳"],"982202121549450840-何小华":["982202121549450840-何小华"],"982202150351312840-玄佳":["982202150351312840-玄佳"],"982202161950556929-巩春霞":["982202161950556929-巩春霞"],"982202170100276326-李树华":["982202170100276326-李树华"],"982202171317458293-谢娅妮":["982202171317458293-谢娅妮"],"982202171557236759-温家健":["982202171557236759-温家健"],"982202171647566059-杨斌":["982202171647566059-杨斌"],"982202180957554523-庞世明":["982202180957554523-庞世明"],"982202191220325205-唐国源":["982202191220325205-唐国源"],"982202200538468127-陈泰安":["982202200538468127-陈泰安"],"982202201210085582-陈龙珠":["982202201210085582-陈龙珠"],"982202210923537313-徐建伟":["982202210923537313-徐建伟"],"982202212035026357-杨小卫":["982202212035026357-杨小卫"],"982202221217070734-宋贵福":["982202221217070734-宋贵福"]}';

        // $clean_data = json_decode($json_str,true);
        // $type = 'file_change_name';
        // $root_dir_path = '/data/nas/数据清洗专用/测试专用/文件renametest/';
        // foreach( $clean_data as $jb => $value)
        // {
        //     $data['dir_path'] = $root_dir_path.$jb;
        //     $result[] = RedisLogic::ins()->addTaskStream($type, $data,false);
        // }
        // return json($result);
    }

    /**
     * 检查缺失文件【无用】
     * @return void
     */
    public function testCheckFilestatus()
    {
         $json_str = '{"862305221644263981-张连彩":["862305221644263981-张连彩"],"862306081507211488-张宏敏":["862306081507211488-张宏敏"],"982012180938502617-宋丽":["982012180938502617-宋丽"],"982105121625500207-王玲":["982105121625500207-王玲"],"982105160229265603-丁仕雷":["982105160229265603-丁仕雷"],"982105191111030217-丁仕雷":["982105191111030217-丁仕雷"],"982105191114180397-隋世磊":["982105191114180397-隋世磊"],"982105221320151883-王魁":["982105221320151883-王魁"],"982105251216594853-吴坤":["982105251216594853-吴坤"],"982105280931587862-王庆":["982105280931587862-王庆"],"982105311632240421-纪艺勇":["982105311632240421-纪艺勇"],"982105311918046273-李千里":["982105311918046273-李千里"],"982106031859542828-宁晓兰":["982106031859542828-宁晓兰"],"982106081157144184-宁晓兰":["982106081157144184-宁晓兰"],"982106122118078369-宋丽":["982106122118078369-宋丽"],"982106190506438588-李晨":["982106190506438588-李晨"],"982106220655390869-宁晓兰":["982106220655390869-宁晓兰"],"982106231356113837-赵瑞江":["982106231356113837-赵瑞江"],"982106241407092342-王庆":["982106241407092342-王庆"],"982106270918347973-隋世磊":["982106270918347973-隋世磊"],"982106290056492064-刘路":["982106290056492064-刘路"],"982107251026472402-潘记明":["982107251026472402-潘记明"],"982107252100242881-李晨":["982107252100242881-李晨"],"982108021449583570-王峰":["982108021449583570-王峰"],"982108041159043948-王峰":["982108041159043948-王峰"],"982108051157327698-王峰":["982108051157327698-王峰"],"982108080915349827-王玉龙":["982108080915349827-王玉龙"],"982108081400115944-李丹明":["982108081400115944-李丹明"],"982108081510071151-刘根":["982108081510071151-刘根"],"982108131331599390-宋丽":["982108131331599390-宋丽"],"982108151429058067-陈安妮":["982108151429058067-陈安妮"],"982108271428063761-钱康宁":["982108271428063761-钱康宁"],"982109051605383041-杨世刚":["982109051605383041-杨世刚"],"982109051640105872-吕富贵":["982109051640105872-吕富贵"],"982109071334307621-杨世刚":["982109071334307621-杨世刚"],"982110071642261739-詹恒":["982110071642261739-詹恒"],"982112040838501088-唐果":["982112040838501088-唐果"],"982112062134494711-黄晓阳":["982112062134494711-黄晓阳"],"982112091908149998-蔺玉婷":["982112091908149998-蔺玉婷"],"982112130928482490-李一博":["982112130928482490-李一博"],"982112181605264186-胡俊宝":["982112181605264186-胡俊宝"],"982112261104076770-孟祥迎":["982112261104076770-孟祥迎"],"982112271300300977-冯无及":["982112271300300977-冯无及"],"982201010927107312-刘克铭":["982201010927107312-刘克铭"],"982201031530041481-向辉映":["982201031530041481-向辉映"],"982201071640238452-徐建伟":["982201071640238452-徐建伟"],"982201091936508606-丁鹏飞":["982201091936508606-丁鹏飞"],"982201102337092197-路晶":["982201102337092197-路晶"],"982201111155295781-张彦龙":["982201111155295781-张彦龙"],"982201131627515010-李康":["982201131627515010-李康"],"982201131857435208-吴海华":["982201131857435208-吴海华"],"982201132323236884-吴育礼":["982201132323236884-吴育礼"],"982201151758252317-时春阳":["982201151758252317-时春阳"],"982201160222565922-黄越高":["982201160222565922-黄越高"],"982201160909351421-罗阳":["982201160909351421-罗阳"],"982201191242189035-梁云庆":["982201191242189035-梁云庆"],"982201241631133052-路静娟":["982201241631133052-路静娟"],"982201242136007233-史开斌":["982201242136007233-史开斌"],"982201250836117285-臧海鹏":["982201250836117285-臧海鹏"],"982201251352035254-史开斌":["982201251352035254-史开斌"],"982201251726006324-倪铭":["982201251726006324-倪铭"],"982201261127359925-郭金龙":["982201261127359925-郭金龙"],"982201261129574898-冯建新":["982201261129574898-冯建新"],"982201261456566219-刘克铭":["982201261456566219-刘克铭"],"982201272348493968-李思凯":["982201272348493968-李思凯"],"982201281450483794-刘克铭":["982201281450483794-刘克铭"],"982201290918112670-樊文亮":["982201290918112670-樊文亮"],"982201291320246417-樊文亮":["982201291320246417-樊文亮"],"982201292154007344-陈泰安":["982201292154007344-陈泰安"],"982201301159553198-陈泰安":["982201301159553198-陈泰安"],"982201310959223329-桂建平":["982201310959223329-桂建平"],"982201311507225142-厉复伟":["982201311507225142-厉复伟"],"982202011020008222-龙正康":["982202011020008222-龙正康"],"982202021343435092-节仲豪":["982202021343435092-节仲豪"],"982202031056361004-曹文洲":["982202031056361004-曹文洲"],"982202031422328811-唐果":["982202031422328811-唐果"],"982202050952001308-汤孟龙":["982202050952001308-汤孟龙"],"982202051350404143-王全扶":["982202051350404143-王全扶"],"982202061004251381-汤凌辉":["982202061004251381-汤凌辉"],"982202061054211558-王泽宇":["982202061054211558-王泽宇"],"982202070521059479-陈义杨":["982202070521059479-陈义杨"],"982202071242059358-王全扶":["982202071242059358-王全扶"],"982202091319397286-谢娅妮":["982202091319397286-谢娅妮"],"982202100907427039-张其前":["982202100907427039-张其前"],"982202101538530935-陈瑜":["982202101538530935-陈瑜"],"982202120304367023-玄佳":["982202120304367023-玄佳"],"982202121549450840-何小华":["982202121549450840-何小华"],"982202150351312840-玄佳":["982202150351312840-玄佳"],"982202161950556929-巩春霞":["982202161950556929-巩春霞"],"982202170100276326-李树华":["982202170100276326-李树华"],"982202171317458293-谢娅妮":["982202171317458293-谢娅妮"],"982202171557236759-温家健":["982202171557236759-温家健"],"982202171647566059-杨斌":["982202171647566059-杨斌"],"982202180957554523-庞世明":["982202180957554523-庞世明"],"982202191220325205-唐国源":["982202191220325205-唐国源"],"982202200538468127-陈泰安":["982202200538468127-陈泰安"],"982202201210085582-陈龙珠":["982202201210085582-陈龙珠"],"982202210923537313-徐建伟":["982202210923537313-徐建伟"],"982202212035026357-杨小卫":["982202212035026357-杨小卫"],"982202221217070734-宋贵福":["982202221217070734-宋贵福"]}';

        $check_data = json_decode($json_str, true);
        $clean_type_data = HandleCompany::ins()->where('id',1)->value('clean_type_data');
        $type_data = array_values(json_decode($clean_type_data, true));
        $type = 'check_file';
        $root_dir_path = '/data/nas/数据清洗专用/测试专用/文件renametest/';
         foreach ($check_data as $jb => $value) {
            $data['dir_path'] = $root_dir_path.$jb;
            $data['type_data'] = $type_data;
            $result[] = RedisLogic::ins()->addTaskStream($type, $data,true);
         }

    }
}